// 共享类型定义

/// 定义DOM遍历的方法
#[derive(PartialEq, Clone, Copy)]
pub enum TraverseMethod {
    /// 深度优先搜索 - 先遍历完整个分支再移动到下一个分支
    DepthFirst,
    /// 广度优先搜索 - 按层级逐层遍历所有节点
    BreadthFirst,
}

// 功能标签页枚举
#[derive(PartialEq, Clone)]
pub enum FeatureTab {
    // HTML 解析器部分
    HtmlParser,
    DomManipulation,
    CssSelector,
    Validation,
    HtmlSerialization,
    FormProcessing,
    AriaSupport,
    DomTraversal,
    LinkExtractor,
    HtmlCleaner,
    SvgSupport,    // 新增SVG支持测试
    MathMLSupport, // 新增MathML支持测试

    // CSS 解析器部分
    CssParser,
    SelectorParser,
    StyleComputation,
    MediaQuery,
    CssAnimation,

    // 布局引擎部分
    BoxModel,
    FlexLayout,
    GridLayout,
    Positioning,

    // 渲染引擎部分
    Rendering,
    TextRendering,
    ImageRendering,

    // 网络模块部分
    HttpClient,
    CookieManager,

    // JavaScript部分
    JsInteraction,
    DomEvents,

    // 用户界面部分
    UiComponents,
}

impl Default for FeatureTab {
    fn default() -> Self {
        FeatureTab::HtmlParser
    }
}

// HTTP方法枚举
#[derive(PartialEq, Clone)]
pub enum HttpMethod {
    Get,
    Post,
    Put,
    Delete,
    Head,
    Options,
    Patch,
    Connect,
    Trace,
}

impl Default for HttpMethod {
    fn default() -> Self {
        HttpMethod::Get
    }
}

// 功能分类枚举
#[derive(PartialEq, Clone)]
pub enum FeatureCategory {
    HtmlParser,
    CssParser,
    LayoutEngine,
    RenderingEngine,
    Network,
    JavaScript,
    UserInterface,
}

impl Default for FeatureCategory {
    fn default() -> Self {
        FeatureCategory::HtmlParser
    }
}

// Cookie数据结构
#[derive(Clone, Debug)]
pub struct CookieData {
    pub name: String,
    pub value: String,
    pub domain: String,
    pub path: String,
    pub expiry: String,
}

impl CookieData {
    pub fn new(name: String, value: String, domain: String, path: String, expiry: String) -> Self {
        Self {
            name,
            value,
            domain,
            path,
            expiry,
        }
    }

    // 检查Cookie是否过期
    pub fn is_expired(&self) -> bool {
        // 在实际实现中，这里应该解析expiry字段并与当前时间比较
        // 这里简化处理，假设"会话结束"表示会话Cookie
        self.expiry == "会话结束"
    }

    // 将Cookie转换为HTTP Cookie头的格式
    pub fn to_header_string(&self) -> String {
        format!("{}={}", self.name, self.value)
    }

    // 检查Cookie是否匹配指定的域名和路径
    pub fn matches(&self, domain: &str, path: &str) -> bool {
        // 这是一个简化的匹配逻辑，实际的Cookie匹配规则更复杂
        self.domain == domain && (self.path == "/" || path.starts_with(&self.path))
    }
}

// JavaScript日志等级枚举
#[derive(Clone, PartialEq)]
pub enum JsLogLevel {
    Info,
    Warning,
    Error,
}

// JavaScript日志结构
#[derive(Clone)]
pub struct JsLog {
    pub level: JsLogLevel,
    pub message: String,
}

impl JsLog {
    pub fn new(level: JsLogLevel, message: String) -> Self {
        Self { level, message }
    }

    pub fn info(message: String) -> Self {
        Self::new(JsLogLevel::Info, message)
    }

    pub fn warning(message: String) -> Self {
        Self::new(JsLogLevel::Warning, message)
    }

    pub fn error(message: String) -> Self {
        Self::new(JsLogLevel::Error, message)
    }

    pub fn get_level_str(&self) -> &str {
        match self.level {
            JsLogLevel::Info => "INFO",
            JsLogLevel::Warning => "WARNING",
            JsLogLevel::Error => "ERROR",
        }
    }

    pub fn get_color(&self) -> [u8; 3] {
        match self.level {
            JsLogLevel::Info => [42, 157, 143],     // 绿色调
            JsLogLevel::Warning => [233, 196, 106], // 黄色调
            JsLogLevel::Error => [231, 111, 81],    // 红色调
        }
    }
}
